using System;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Visuals;
using FilteredStatistics.Controller;
using FilteredStatistics.Data;

namespace FilteredStatistics.View
{
    partial class AddRenameTemplateDialog : Form
    {
        public AddRenameTemplateDialog(Template template)
        {
            InitializeComponent();

            m_TemplateToRename = template;

            NewTemplateNameLabel.Text = PluginView.GetLocalizedString("NewTemplateNameLabelText");

            if (m_TemplateToRename != null)
            {
                this.Text = PluginView.GetLocalizedString("RenameTemplateDialogText");
                TemplateNameTextBox.Text = m_TemplateToRename.Name;
            }
            else
            {
                this.Text = PluginView.GetLocalizedString("NewTemplateText");
                TemplateNameTextBox.Text = PluginView.GetLocalizedString("NewTemplateText");
            }

            OKButton.Text = CommonResources.Text.ActionOk;
            Cancel_Button.Text = CommonResources.Text.ActionCancel;
        }

        private void TemplateNameTextBox_TextChanged(object sender, EventArgs e)
        {
            OKButton.Enabled = TemplateNameTextBox.Text != "";
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            bool close = true;

            if (TemplateNameTextBox.Text == "")
            {
                close = false;

                MessageBox.Show(PluginView.GetLocalizedString("MustEnterNameText"),
                                PluginView.GetLocalizedString("ErrorText"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                Template foundTemplate = TemplateManager.Instance.GetTemplate(TemplateNameTextBox.Text);

                if (foundTemplate != null && foundTemplate != m_TemplateToRename)
                {
                    close = false;

                    MessageBox.Show(PluginView.GetLocalizedString("MustEnterNameText"),
                                    PluginView.GetLocalizedString("ErrorText"),
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            if (close)
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public String TemplateName
        {
            get
            {
                return TemplateNameTextBox.Text;
            }
        }

        private Template m_TemplateToRename = null;
    }
}